package cn.song.docflow.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * Web MVC 配置
 *
 * @author SongRenShuo
 * @date 2023/05/20
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    /** 导出目录 */
    @Value("${file.export-dir:exports/}")
    private String exportDir;

    /** 上传目录 */
    @Value("${file.upload-dir:uploads/}")
    private String uploadDir;

    /** 模板目录 */
    @Value("${file.template-dir:templates/}")
    private String templateDir;

    /**
     * 配置静态资源映射
     *
     * @param registry 资源处理器注册表
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        // 使用绝对路径配置uploads目录
        try {
            File uploadDirFile = new File(uploadDir).getAbsoluteFile();
            if (!uploadDirFile.exists()) {
                uploadDirFile.mkdirs();
            }
            String uploadPath = uploadDirFile.toURI().toString();
            registry.addResourceHandler("/uploads/**")
                    .addResourceLocations(uploadPath);
        } catch (Exception e) {
            // 如果获取绝对路径失败，回退到原来的配置
            registry.addResourceHandler("/uploads/**")
                    .addResourceLocations("file:./uploads/");
        }

        // 使用绝对路径配置exports目录
        try {
            File exportDirFile = new File(exportDir).getAbsoluteFile();
            if (!exportDirFile.exists()) {
                exportDirFile.mkdirs();
            }
            String exportPath = exportDirFile.toURI().toString();
            registry.addResourceHandler("/exports/**")
                    .addResourceLocations(exportPath);
        } catch (Exception e) {
            // 如果获取绝对路径失败，回退到原来的配置
            registry.addResourceHandler("/exports/**")
                    .addResourceLocations("file:./exports/");
        }

        // 使用绝对路径配置templates目录
        try {
            File templateDirFile = new File(templateDir).getAbsoluteFile();
            if (!templateDirFile.exists()) {
                templateDirFile.mkdirs();
            }
            String templatePath = templateDirFile.toURI().toString();
            registry.addResourceHandler("/templates/**")
                    .addResourceLocations(templatePath);
        } catch (Exception e) {
            // 如果获取绝对路径失败，回退到原来的配置
            registry.addResourceHandler("/templates/**")
                    .addResourceLocations("file:./templates/");
        }
    }

    /**
     * 配置跨域请求
     *
     * @param registry 跨域注册表
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
}
